/*  JInternalFrameIniciarJogo.java
 *  Copyright (C) 2009  Tiago Silva Loureiro
 *  This File is part of Bier Manager.
 *  Bier Manager a is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/*  JInternalFrameIniciarJogo.java - Bier Manager
 *
 *  Tela para se começar ou continuar uma temporada do Bier Manager.
 *  Contém os componentes de segurança do usuário para que outros
 *  não utilizem sua configuração de jogo sem o seu conhecimento.
 *  Abre o banco de dados de contas e verifica se o usuario digitado
 *  corresponde a algum usuário cadastrado para iniciar o jogo.
 *  @author Tiago Silva Loureiro
 * *
 */

package biermanager.interfaceGrafica;

import biermanager.nucleo.ConstantesDeCaminho;
import biermanager.nucleo.Usuario;
import biermanager.componentes.Eventos;
import biermanager.componentes.PintorDeBackground;
import biermanager.componentes.PintorDeForeground;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;
import org.jdesktop.swingx.painter.AbstractLayoutPainter.HorizontalAlignment;
import org.jdesktop.swingx.painter.AbstractLayoutPainter.VerticalAlignment;
import org.jdesktop.swingx.painter.CompoundPainter;
import org.jdesktop.swingx.painter.ImagePainter;


public class JInternalFrameIniciarJogo extends javax.swing.JInternalFrame{
    
    private float alpha;
    private PintorDeBackground pintor;
    
    public JInternalFrameIniciarJogo(){
        initComponents();
        iniciarDisplay();
        iniciarBackgrounds();
        iniciarForegrounds();
    }

    private void iniciarDisplay(){
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation( (int)( ( screenSize.getWidth() - this.getWidth() ) / 2 ), 50 );
        ((javax.swing.plaf.basic.BasicInternalFrameUI) this.getUI()).setNorthPane(null);
        this.setDefaultCloseOperation(JInternalFrameIniciarJogo.DO_NOTHING_ON_CLOSE);
    }
    private void iniciarBackgrounds(){
        pintor = new PintorDeBackground();
        ImagePainter imp = (ImagePainter) pintor.pintorIcone(ConstantesDeCaminho.chaves, 100, 100);
        imp.setHorizontalAlignment(HorizontalAlignment.LEFT);
        imp.setVerticalAlignment(VerticalAlignment.BOTTOM);
        jxPainelEstrutural.setBackgroundPainter(new CompoundPainter(pintor.pintarDegradePreto(jxPainelEstrutural),imp));
        jxlabelBotaoIniciarJogo.setBackgroundPainter(pintor.pintarBotaoSemFocoCapsulaPreto());
        jxlabelBotaoFechar.setBackgroundPainter(pintor.pintarCinzaTransparente(jxlabelBotaoFechar));
    }
    private void iniciarForegrounds(){
        jxlabelBotaoIniciarJogo.setForeground(PintorDeForeground.corBotao());
        jxlabelBotaoFechar.setForeground(PintorDeForeground.corFechar());
    }

    protected void setAlpha(){
        this.jxPainelEstrutural.setAlpha(alpha);
    }

    /**
     * Gerado automaticamente pelo matisse( Editor visual do NetBeans ).
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jxPainelInvisivel = new org.jdesktop.swingx.JXPanel();
        jxPainelEstrutural = new org.jdesktop.swingx.JXPanel();
        jlabelFacaLogin = new javax.swing.JLabel();
        jlabelNome = new javax.swing.JLabel();
        jlabelSenha = new javax.swing.JLabel();
        jtextfieldNome = new javax.swing.JTextField();
        jpasswordfieldSenha = new javax.swing.JPasswordField();
        jlabelImagemTopo = new javax.swing.JLabel();
        jxlabelBotaoIniciarJogo = new org.jdesktop.swingx.JXLabel();
        jxlabelBotaoFechar = new org.jdesktop.swingx.JXLabel();

        setBackground(new java.awt.Color(0, 0, 0));
        setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), javax.swing.BorderFactory.createLineBorder(new java.awt.Color(102, 102, 102))));
        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(385, 215));
        setRequestFocusEnabled(false);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });
        addContainerListener(new java.awt.event.ContainerAdapter() {
            public void componentRemoved(java.awt.event.ContainerEvent evt) {
                formComponentRemoved(evt);
            }
        });

        jxPainelInvisivel.setAlpha(0f);
        jxPainelInvisivel.setBackground(new java.awt.Color(235, 0, 0));
        jxPainelInvisivel.setOpaque(false);
        jxPainelInvisivel.setPreferredSize(new java.awt.Dimension(385, 215));

        jxPainelEstrutural.setAlpha(0.f);
        jxPainelEstrutural.setBackground(new java.awt.Color(204, 204, 204));
        jxPainelEstrutural.setInheritAlpha(false);
        jxPainelEstrutural.setOpaque(false);
        jxPainelEstrutural.setAlignmentX(0.0F);
        jxPainelEstrutural.setAlignmentY(0.0F);
        jxPainelEstrutural.setPreferredSize(new java.awt.Dimension(385, 215));

        jlabelFacaLogin.setFont(new java.awt.Font("Tahoma", 1, 16));
        jlabelFacaLogin.setText("Faça o seu Login para iniciar o Bier Manager!");
        jlabelFacaLogin.setForeground(new java.awt.Color(255, 255, 255));

        jlabelNome.setBackground(new java.awt.Color(255, 255, 255));
        jlabelNome.setFont(new java.awt.Font("Tahoma", 1, 12));
        jlabelNome.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jlabelNome.setText("Nome de Usuário:");
        jlabelNome.setForeground(new java.awt.Color(255, 255, 255));
        jlabelNome.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        jlabelSenha.setBackground(new java.awt.Color(255, 255, 255));
        jlabelSenha.setFont(new java.awt.Font("Tahoma", 1, 12));
        jlabelSenha.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jlabelSenha.setText("Senha:");
        jlabelSenha.setForeground(new java.awt.Color(255, 255, 255));

        jtextfieldNome.setFont(new java.awt.Font("Tahoma", 1, 12));
        jtextfieldNome.setOpaque(false);
        jtextfieldNome.setToolTipText("Digite o seu nome de usuário.");
        jtextfieldNome.setCaretColor(new java.awt.Color(255, 255, 255));

        jpasswordfieldSenha.setColumns(8);
        jpasswordfieldSenha.setFont(new java.awt.Font("Tahoma", 1, 12));
        jpasswordfieldSenha.setOpaque(false);
        jpasswordfieldSenha.setToolTipText("Digite a sua senha.");
        jpasswordfieldSenha.setCaretColor(new java.awt.Color(255, 255, 255));
        jpasswordfieldSenha.setEchoChar('*');
        jpasswordfieldSenha.setSelectionEnd(7);

        jlabelImagemTopo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/biermanager/imagens/iniciarjogo.png"))); // NOI18N

        jxlabelBotaoIniciarJogo.setForeground(new java.awt.Color(20, 20, 20));
        jxlabelBotaoIniciarJogo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jxlabelBotaoIniciarJogo.setText("Iniciar Jogo!");
        jxlabelBotaoIniciarJogo.setFont(new java.awt.Font("Tahoma", 1, 12));
        jxlabelBotaoIniciarJogo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jxlabelBotaoIniciarJogoMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jxlabelBotaoIniciarJogoMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jxlabelBotaoIniciarJogoMouseExited(evt);
            }
        });

        jxlabelBotaoFechar.setForeground(new java.awt.Color(255, 255, 255));
        jxlabelBotaoFechar.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jxlabelBotaoFechar.setText("x");
        jxlabelBotaoFechar.setFont(new java.awt.Font("Tahoma", 1, 14));
        jxlabelBotaoFechar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jxlabelBotaoFechar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jxlabelBotaoFecharMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jxlabelBotaoFecharMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jxlabelBotaoFecharMouseExited(evt);
            }
        });

        javax.swing.GroupLayout jxPainelEstruturalLayout = new javax.swing.GroupLayout(jxPainelEstrutural);
        jxPainelEstrutural.setLayout(jxPainelEstruturalLayout);
        jxPainelEstruturalLayout.setHorizontalGroup(
            jxPainelEstruturalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jxPainelEstruturalLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jxPainelEstruturalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jxPainelEstruturalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jlabelFacaLogin)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jxPainelEstruturalLayout.createSequentialGroup()
                            .addGroup(jxPainelEstruturalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jlabelSenha, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jlabelNome, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jxPainelEstruturalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jxlabelBotaoIniciarJogo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jpasswordfieldSenha)
                                .addComponent(jtextfieldNome, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE))))
                    .addGroup(jxPainelEstruturalLayout.createSequentialGroup()
                        .addComponent(jlabelImagemTopo, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 114, Short.MAX_VALUE)
                        .addComponent(jxlabelBotaoFechar, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jxPainelEstruturalLayout.setVerticalGroup(
            jxPainelEstruturalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jxPainelEstruturalLayout.createSequentialGroup()
                .addGroup(jxPainelEstruturalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jxPainelEstruturalLayout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addComponent(jlabelImagemTopo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jxlabelBotaoFechar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jlabelFacaLogin)
                .addGap(26, 26, 26)
                .addGroup(jxPainelEstruturalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlabelNome)
                    .addComponent(jtextfieldNome, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jxPainelEstruturalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlabelSenha)
                    .addComponent(jpasswordfieldSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jxlabelBotaoIniciarJogo, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout jxPainelInvisivelLayout = new javax.swing.GroupLayout(jxPainelInvisivel);
        jxPainelInvisivel.setLayout(jxPainelInvisivelLayout);
        jxPainelInvisivelLayout.setHorizontalGroup(
            jxPainelInvisivelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jxPainelInvisivelLayout.createSequentialGroup()
                .addComponent(jxPainelEstrutural, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jxPainelInvisivelLayout.setVerticalGroup(
            jxPainelInvisivelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jxPainelEstrutural, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jxPainelInvisivel, javax.swing.GroupLayout.PREFERRED_SIZE, 385, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jxPainelInvisivel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jxlabelBotaoIniciarJogoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jxlabelBotaoIniciarJogoMouseClicked
                this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
            char senhaTemp[] =  jpasswordfieldSenha.getPassword();
            Usuario usuario = new Usuario( jtextfieldNome.getText());
            Arrays.fill(senhaTemp,'0');
/*          Registro r = new Registro();
        try {
            if (r.validaUsuario(usuario)) {
                firePropertyChange("Iniciar", false, true);
                this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
            }
            else{
                emissorDeSons.play(erro);
                JOptionPane.showMessageDialog(null, "Login ou senha incorreta!");
                this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
            }
        }catch (BadPaddingException ex) {Logger.getLogger(JInternalFrameIniciarJogo.class.getName()).log(Level.SEVERE, null, ex);} catch (IllegalBlockSizeException ex) {Logger.getLogger(JInternalFrameIniciarJogo.class.getName()).log(Level.SEVERE, null, ex);} catch (InvalidKeyException ex) {Logger.getLogger(JInternalFrameIniciarJogo.class.getName()).log(Level.SEVERE, null, ex);} catch (NoSuchAlgorithmException ex) {Logger.getLogger(JInternalFrameIniciarJogo.class.getName()).log(Level.SEVERE, null, ex);} catch (InvalidAlgorithmParameterException ex) {Logger.getLogger(JInternalFrameIniciarJogo.class.getName()).log(Level.SEVERE, null, ex);} catch (NoSuchPaddingException ex) {Logger.getLogger(JInternalFrameIniciarJogo.class.getName()).log(Level.SEVERE, null, ex);}*/

}//GEN-LAST:event_jxlabelBotaoIniciarJogoMouseClicked

private void jxlabelBotaoIniciarJogoMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jxlabelBotaoIniciarJogoMouseEntered
    Eventos.mouseEntraBotao(evt);
}//GEN-LAST:event_jxlabelBotaoIniciarJogoMouseEntered

private void jxlabelBotaoIniciarJogoMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jxlabelBotaoIniciarJogoMouseExited
    Eventos.mouseSaiBotao(evt);
}//GEN-LAST:event_jxlabelBotaoIniciarJogoMouseExited
    
private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
    alpha = 0.f;
    final Timer tempo = new Timer();            //Temporizador
    TimerTask tarefa = new TimerTask() {     //Objeto tarefa a ser realizada

        public void run() {                 //Metodo run que implementa a tarefa
            if ((alpha + 0.05) <= 1.0f) {
                alpha = alpha + 0.05f;
                
            } else {
                tempo.cancel();
                
            }
            setAlpha();
        }
    };
    tempo.schedule(tarefa, 50, 5);
}//GEN-LAST:event_formComponentShown

private void formComponentRemoved(java.awt.event.ContainerEvent evt) {//GEN-FIRST:event_formComponentRemoved
    this.dispose();
}//GEN-LAST:event_formComponentRemoved

private void jxlabelBotaoFecharMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jxlabelBotaoFecharMouseEntered
    Eventos.mouseEntraFechar(evt);
}//GEN-LAST:event_jxlabelBotaoFecharMouseEntered

private void jxlabelBotaoFecharMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jxlabelBotaoFecharMouseExited
    Eventos.mouseSaiFechar(evt);
}//GEN-LAST:event_jxlabelBotaoFecharMouseExited

private void jxlabelBotaoFecharMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jxlabelBotaoFecharMouseClicked
    firePropertyChange("Fechar", null, null);
}//GEN-LAST:event_jxlabelBotaoFecharMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jlabelFacaLogin;
    private javax.swing.JLabel jlabelImagemTopo;
    private javax.swing.JLabel jlabelNome;
    private javax.swing.JLabel jlabelSenha;
    private javax.swing.JPasswordField jpasswordfieldSenha;
    private javax.swing.JTextField jtextfieldNome;
    private org.jdesktop.swingx.JXPanel jxPainelEstrutural;
    private org.jdesktop.swingx.JXPanel jxPainelInvisivel;
    private org.jdesktop.swingx.JXLabel jxlabelBotaoFechar;
    private org.jdesktop.swingx.JXLabel jxlabelBotaoIniciarJogo;
    // End of variables declaration//GEN-END:variables

}
